/*
 * TestDriver.java
 *
 * Created on 6 June 2007, 13:38
 * Copyright (C) 2007   Yuezhong Zhang
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package usyd.comp5348;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Yuezhong Zhang
 */
public class TestDriver {
    private Logger logger = Logger.getLogger(getClass().getName());
    /** Creates a new instance of TestDriver */
    public TestDriver() {
    }
    public static void main(String[] args){
        TestDriver td = new TestDriver();
        td.test();
    }
    public void test(){
        Customer cust = new Customer();
        cust.setName("Yuezhong");
        cust.setAddress("Darlington 90");
        cust.setCardName("Yuezhong");
        cust.setCardNumber("12-123-111");
        cust.setCardExpireDate("11-09-07");
        CustomerFacadeRemote cf = lookupCustomer();
        cf.createCustomer(cust);
        logger.log(Level.INFO,"Created a customer");

        Catalogue cat = new Catalogue();
        cat.setCatalogueNumber("1234");
        cat.setTitle("Spiderman 3");
        cat.setDirector("Sam Raimi");
        cat.setGenre("Action");
        cat.setPrice(12);
        CatalogueFacadeRemote catf =  lookupCatalogueFacadeBean();
        catf.createVideo(cat);

        logger.log(Level.INFO,"Insert a new video");



        Stock s = new Stock();
        s.setCatalogueId(1234);
        s.setHolding(20);
        s.setWarehouse("SydneyWarehouse1");

        StockFacadeRemote  sfr = lookupStock();

        sfr.createItem(s);
        // if exception happen, this won't print out'
        logger.log(Level.INFO,"Created item in the stock");
        Order order = new Order();
        order.setCustomerId(cust.getId());
        order.setOrderDate(new Date());
        order.setStatus(Order.PENDING);
        order.setStore("SydneyShop1");
        order.setWarehouse("SydneyWarehouse1");
        OrderLine ol = new OrderLine();
        ol.setCatalogueId(1234);
        ol.setOrderId(order);
        ol.setQuantity(2);
        order.getVideos().add(ol);
        OrderFacadeRemote of = lookupOrder();
        try {
         of.createOrder(order);
            logger.log(Level.INFO,"created order");
        } catch (OrderException ex) {
            ex.printStackTrace();
            logger.log(Level.SEVERE,"Failed to create order", ex);
        }

        try {
            of.submitOrder(order.getId());
            logger.log(Level.INFO,"successfully submitted order");
        } catch (OrderException ex) {
            ex.printStackTrace();
            logger.log(Level.SEVERE,"Failed to submit order, try another warehouse", ex);
        }

    }
    private CatalogueFacadeRemote lookupCatalogueFacadeBean() {
        try {
            logger.info("lookup for Catalogue component");
            Context c = new InitialContext();
            return (CatalogueFacadeRemote) c.lookup("java:comp/env/ejb/Catalogue");
        } catch(NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        }
    }
    
    private OrderFacadeRemote lookupOrder() {
        try {
            logger.info("lookup for Order component");
            Context c = new InitialContext();
            return (OrderFacadeRemote) c.lookup("java:comp/env/ejb/Order");
        } catch(NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        }
    }
    
    private CustomerFacadeRemote lookupCustomer() {
        try {
            logger.info("lookup for Customer component");
            Context c = new InitialContext();
            return (CustomerFacadeRemote) c.lookup("java:comp/env/ejb/Customer");
        } catch(NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        }
    }
    
    private StockFacadeRemote lookupStock() {
        try {
            Context c = new InitialContext();
            return (StockFacadeRemote) c.lookup("java:comp/env/ejb/Stock");
        } catch(NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        }
    }
}
